
/**
 * 创建时间: 2020/6/28 11:47
 * 作者:lixu
 * 功能描述:处理server推送的消息(包含resp和push)
 * 修改时间:
 * 修改描述:
 */
//: PushNotifyDelegate
public class PushHandler {
    private let TAG = String(describing:PushHandler.self)

//    public func PushHandler(looper:Looper) {
//        super(looper);
//    }
    
    
    /**
     * 处理接收到的消息
     *
     * @param cmdid      消息标示{@link ReqCmdID}
     * @param data       消息内容
     * @param imCallback 回调
     */

    
//    public func notifyPushMessage(_ pushData:[UInt8], withCmdId cmdId: Int32) {
//        onPush(cmdid: Int(cmdId), data: pushData , imCallback: nil)
//    }
    
    public func onPush (cmdid:Int, data:Data,  imCallback:AnyObject?) {
        let json:String = ProtocolUtil.getReceiveMsgContent(buffer: [UInt8](data));
        if json.count==0 {
            //TODO 
            IMLog.i(tag:TAG,msg:"data == nil");
            return
        }
        IMLog.i(tag:TAG,msg:"onPush cmdId:\(cmdid) length:\(data.count) data:\(json)");
            
        let msgId = AdapterTgameUtils.getRespMsgId(isTgame: true, json: json);
       
        //获取msgId字段,判断当前消息是否是服务器响应
        let  respBean:RespBean = MsgUtils.parseToMsgRespBean(json: json);
             respBean.msgId = msgId;
        //新的逻辑
        let isServerRespMsg  = MsgUtils.isServerRespMsg(data: data);
        
        
        print("isServerRespMsg =");
//        CommandUtils.isServerResp(cmdId: cmdid)  旧的判断方法
        if (isServerRespMsg) {
            //服务器响应的消息
//            let isSucc:Bool = CommandUtils.isSuccessResp(cmdId: cmdid);
            
            TaskHandler.getInstance().removeTask(cmdid: cmdid, isSuccess: isServerRespMsg, respBean: respBean, resultJson: json);

        } else {
            //服务器推送的消息
            let  roomId:String = MsgUtils.parseToRoomId(json: json);
            
            if (imCallback != nil) {
                let callback = imCallback as! IMsgCallBack
                callback.onReceivedMsg(roomId: roomId, cmdId: cmdid, contentJson: json);
            }

        }
    }
}
